﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using DAL;
namespace BIZ
{
    public class ArticleBIZ
    {
        public List<ARTICLE> getAll()
        {
            return WebUtils.db.ARTICLEs.ToList();
        }
        
        public void edit(ARTICLE art, Guid id)
        {
            var a = WebUtils.db.ARTICLEs.Single(artic => artic.ArticleID.Equals(id));

            a.Title = art.Title;
            a.Url = art.Url;
            a.Content = art.Content;
            
            WebUtils.db.SubmitChanges();
            
            UrlBIZ.GetAllUrl();
        }

        public void ChangeStatus(Guid id, bool IsLock)
        {
            ARTICLE a = WebUtils.db.ARTICLEs.Single(artic => artic.ArticleID.Equals(id));
            a.IsLock = IsLock;

            WebUtils.db.SubmitChanges();
        }

        public ARTICLE getId(Guid id)
        {
            //Tương Tự: WebUtils.db.ARTICLEs.Single(...) nhưng dài dòng
            var art = from ar in WebUtils.db.ARTICLEs
                    where ar.ArticleID.Equals(id)
                    select ar;
            
            ARTICLE article = art.FirstOrDefault();
            return article;
        }

        public ARTICLE getIdForUser(Guid id)
        {
            var art = from ar in WebUtils.db.ARTICLEs
                    where ar.ArticleID.Equals(id) && ar.IsLock == false
                    select ar;
            
            ARTICLE article = art.FirstOrDefault();
            return article;
        }
        
        public void insert(ARTICLE art)
        {
            art.ArticleID = Guid.NewGuid();
            art.CreateDate = DateTime.Now;

            WebUtils.db.ARTICLEs.InsertOnSubmit(art);
            WebUtils.db.SubmitChanges();

            UrlBIZ.GetAllUrl();
        }

        public List<ARTICLE> Search(string text)
        {
            var list = (from p in WebUtils.db.ARTICLEs where p.Title.Contains(text) && p.IsLock == false select p).ToList();

            return list;
        }
    }
}
